package com.bw.module_video_detail.test_mediaplayer

import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.module_video_detail.R

/**
 * mediaPlayer+SurfaceView：视频播放
 *
 *
 */
class SurfaceViewActivity : AppCompatActivity(),SurfaceHolder.Callback {
    private lateinit var surfaceView: SurfaceView
    private lateinit var mediaPlayer: MediaPlayer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_surface_view)
        surfaceView = findViewById(R.id.surface_view)
        initPlayer()
        initSurface()

    }

    private fun initSurface() {
        //设置回调
        surfaceView.holder.addCallback(this)
    }

    private fun initPlayer(){
        mediaPlayer = MediaPlayer()
        mediaPlayer.setDataSource("http://txmov2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_b_B09a31110bdc2b61828d27ef27390ecec.mp4?tag=1-1599270586-vdg-1-h2fj3f9rtd-6b0b801241e3b41e&tt=b&bp=10000")
        mediaPlayer.prepareAsync()
        mediaPlayer.setOnPreparedListener {
            mediaPlayer.start()
        }
    }

    /**
     * surface创建
     */
    override fun surfaceCreated(holder: SurfaceHolder) {
        Log.d("TAG", "surfaceCreated: ")
        mediaPlayer.setDisplay(holder) //mediaPlayer将视频渲染到surface
    }

    /**
     * surface大小改变
     */
    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        Log.d("TAG", "surfaceChanged: ")
    }

    /**
     * surface销毁
     */
    override fun surfaceDestroyed(holder: SurfaceHolder) {
        Log.d("TAG", "surfaceDestroyed: ")
        mediaPlayer.release()
    }
}